package com.sparrow.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import com.sparrow.constant.COMMON;
import com.sparrow.core.Log;

public class ConvertEncoding {
	private static boolean isTest = true;
	private static String sourceEncoding = "gb2312";
	private static String descEncoding = "UTF-8";

	/**
	 * @author zlz
	 * 
	 * @time 2013-7-16上午10:26:40
	 * @param args
	 */
	public static void main(String[] args) {

		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
		String rootPath = null;
		String yn = null;
		System.out.println("请输入要转换的路径");
		try {
			rootPath = bf.readLine();
		} catch (IOException e) {
		}

		Convert(rootPath);
		System.out.println("请输入文件的当前编码,回车默认为gbk");
		try {
			String tempEncoding = bf.readLine();
			if (!"".equals(tempEncoding)) {
				sourceEncoding = bf.readLine();
			}
		} catch (IOException e) {
		}

		System.out.println("请输入目标文件编码，回车默认为utf-8");
		try {
			String tempEncoding = bf.readLine();
			if (!"".equals(tempEncoding)) {
				descEncoding = bf.readLine();
			}
		} catch (IOException e) {
		}

		System.out.println("立即生成吗？Y/N");

		try {
			yn = bf.readLine();
		} catch (IOException e) {
		}

		if (yn.toLowerCase().equals("y")) {
			isTest = false;
			Convert(rootPath);
			System.out.println("ending");
		}
	}

	public static void Convert(String path) {
		File file = new File(path);
		File[] files = file.listFiles();
		for (File f : files) {
			if (f.isDirectory() && !f.isHidden()) {
				Convert(f.getPath());
			} else {
				String fileName = f.getPath();
				String sourceText = readFileContent(fileName, sourceEncoding);
				if (fileName.endsWith(".java")) {
					if (isTest) {
						System.out.println(sourceText);
					} else {
						writeFile(fileName, sourceText, descEncoding);
						System.out.println(fileName + " is encoded");
					}
				}
			}
		}
	}

	/**
	 * 以行为单位读取文件，常用于读面向行的格式化文件
	 */
	public static String readFileContent(String fileName, String charset) {
		if (StringUtil.isNullOrEmpty(charset)) {
			charset = "UTF-8";
		}
		File file = new File(fileName);
		BufferedReader reader = null;
		StringBuffer sb = new StringBuffer();
		try {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(file), charset));
			String tempString = null;
			while ((tempString = reader.readLine()) != null) {
				sb.append(tempString);
				sb.append(COMMON.ENTER_TEXT);
			}
			reader.close();
		} catch (IOException e) {
			Log.getInstance().error(e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
		return sb.toString();
	}

	public static boolean writeFile(String filePath, String s, String charset) {
		OutputStreamWriter osw = null;
		try {
			osw = new OutputStreamWriter(new FileOutputStream(filePath),
					charset);
			osw.write(s, 0, s.length());
			osw.flush();
			return true;
		} catch (Exception e) {
			Log.getInstance().error(e);
			return false;
		} finally {
			if (osw != null) {
				try {
					osw.close();
				} catch (IOException e) {
				}
			}
		}
	}

}
